<template>
  <div>
    <el-dialog
      v-loading.lock="loading"
      class="base-dialog"
      :custom-class="'panna-dialog_small'"
      title="报告撤回"
      align="center"
      :visible.sync="innerWithdrawalDialogFormVisible"
      @closed="closeRecordDialog"
    >
      <el-form>
        <el-form-item label="撤回至:">
          <el-select v-model="formData.withdrawalLevel" style="width: 80%">
            <el-option v-if="type === 0" label="报告生成" value="2"></el-option>
            <el-option label="数据录入" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" @click="onSubmit">确 定</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 编辑页面 -->
  </div>
</template>

<script>
import { exportWithdrawal } from "../../api/Report/ReportManage";
export default {
  name: "ReportWithdrawalDialog",
  components: {},
  props: {
    reportIds: {
      type: Array,
      default: () => [],
    },
    sampleRegistrationIds: {
      type: Array,
      default: () => [],
    },
    withdrawalDialogFormVisible: {
      type: Boolean, // 布尔
      default: false, // 必填
    },
    type: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      tableData: [],
      formData: {
        withdrawalLevel: "1",
      },
      loading: false,
      innerWithdrawalDialogFormVisible: false,
    };
  },
  watch: {
    withdrawalDialogFormVisible: function() {
      this.innerWithdrawalDialogFormVisible = this.withdrawalDialogFormVisible;
    },
  },
  created() {
    this.innerWithdrawalDialogFormVisible = this.withdrawalDialogFormVisible;
  },
  methods: {
    // 取消
    closeDialog() {
      this.closeRecordDialog(0);
    },
    // 确定
    onSubmit() {
      this.$confirm("是否确定撤回?", "报告撤回", {
        type: "warning",
        confirmButtonText: "确认",
        okType: "danger",
        center: true,
        cancelButtonText: "取消",
        beforeClose: (action, instance, done) => {
          if (action === "confirm") {
            instance.confirmButtonLoading = true;
            exportWithdrawal({
              reportIds: this.reportIds,
              sampleRegistrationIds: this.sampleRegistrationIds,
              withdrawalLevel: this.formData.withdrawalLevel,
            })
              .then(() => {
                done();
                instance.confirmButtonLoading = false;
                this.$message.success("撤回成功");
                this.closeRecordDialog();
              })
              .catch(() => {
                instance.confirmButtonLoading = false;
                done();
              });
          } else {
            instance.confirmButtonLoading = false;
            done();
          }
        },
      }).catch();
    },

    closeRecordDialog(state) {
      this.innerWithdrawalDialogFormVisible = false;
      this.$parent.getList();
      this.$emit("update:withdrawalDialogFormVisible", false);
    },
  },
};
</script>
